package demo4.util;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

@Component
@PropertySource("classpath:config.properties")
public class dbutil implements idbutil
{
    @Value("${db.url}")
    private String url;
    @Value("${db.username}")
    private String username;
    @Value("${db.password}")
    private String password;
    @Value("${db.driver}")
    private String classname;
    public  void setDriverClass(String classname) {
        System.out.println("正在加载驱动...");
        try {
            Class.forName(classname);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    public void setUrl(String url) {this.url = url;}
    public void setuUsername(String username) {System.out.println("正在初始化username...");this.username = username;}
    public void setPassword(String password) {this.password = password;}

    @Override
    public Connection getConnection() throws SQLException {
        System.out.println("Util正在连接...");
//        return null;
        return DriverManager.getConnection(url,username,password);
    }
}
